﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using System.Collections;

namespace SPVisualDev.VSAddin.Dialogs.PropertySheets
{
    public class ResourceExpandableObjectConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            ResourceStringContainer container = value as ResourceStringContainer;
            IPropertySheet prop_sheet = context.Instance as IPropertySheet;

            if (!ResourceHelper.IsResourceString(container.RawString))
            {
                return null;
            }

            List<ResourceHelper.CultureFileInfo> list = ResourceHelper.GetAllAvailableCultures(container.RawString, prop_sheet.DefaultResourceFile, prop_sheet.ParentFeatureFolder);

            if (list.Count == 0)
            {
                ResourcePropertyDescriptor res_prop_descr = new ResourcePropertyDescriptor("No default resource file could be found.", attributes);
                res_prop_descr.SetReadOnlyFlag(true);

                return new PropertyDescriptorCollection(new ResourcePropertyDescriptor[] { res_prop_descr });
            }


            list.Sort(new ResourceCultureInfoComparer());

            ArrayList arr = new ArrayList();
            foreach (ResourceHelper.CultureFileInfo cFileInfo in list)
            {
                ResourcePropertyDescriptor res_prop_descr;
                if (cFileInfo.Culture.Name == string.Empty)
                {
                    res_prop_descr = new ResourcePropertyDescriptor("(Default)", attributes);
                }
                else
                {
                    res_prop_descr = new ResourcePropertyDescriptor(cFileInfo.Culture.Name + " " + cFileInfo.Culture.DisplayName, attributes);
                }

                if (cFileInfo.ProjectItem == null)
                {
                    res_prop_descr.SetReadOnlyFlag(true);
                }

                arr.Add(res_prop_descr);

            }

            PropertyDescriptorCollection coll = new PropertyDescriptorCollection(null);
            foreach (PropertyDescriptor d in arr)
                coll.Add(d);

            return coll;
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (destinationType == typeof(string));
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(System.String))
                return true;
            else
                return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            object ret;
            if (value is string)
            {
                ResourceStringContainer container = context.PropertyDescriptor.GetValue(context.Instance) as ResourceStringContainer;
                string raw_string = container.RawString;

                ret = new ResourceStringContainer(value as string, container.DefaultResourceFile, container.ParentFeatureFolder);

            }
            else
            {
                ret = base.ConvertFrom(context, culture, value);
            }

            return ret;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value is ResourceStringContainer)
            {
                ResourceStringContainer container = value as ResourceStringContainer;
                if (!ResourceHelper.IsResourceString(container.RawString))
                {
                    context.GetType().GetProperty("Expanded").SetValue(context, true, null);
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        private class ResourceCultureInfoComparer : IComparer<ResourceHelper.CultureFileInfo>
        {
            #region IComparer<CultureInfo> Members

            public int Compare(ResourceHelper.CultureFileInfo x, ResourceHelper.CultureFileInfo y)
            {
                return x.Culture.Name.CompareTo(y.Culture.Name);
            }

            #endregion
        }
    }

}
